// PROGRAMA : IDCPC000(PROGRAMA BATCH)
// BIBLIOTECA : SISP
// OBJETIVO : GRAVA ARQUIVO DE CONTROLE NO ARQUIVO ADABAS
// SIC-CONTROLE-IMP
// OBSERVACAO : JCL = PDS.SEJUSP(CPCL)

package br.com.vti.sisp_sicrim.antigo.controller.programa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.vti.sisp_sicrim.antigo.model.SicControleImp;
import br.com.vti.sisp_sicrim.antigo.util.ExceptionProgram;
import br.com.vti.sisp_sicrim.antigo.util.Sessao;
import br.com.vti.sisp_sicrim.antigo.util.UtilBean;

/**
 * Classe : Idcpc000 OBJETIVO : GRAVA ARQUIVO DE CONTROLE NO ARQUIVO ADABAS SIC-CONTROLE-IMP OBSERVACAO : JCL = PDS.SEJUSP(CPCL) *
 * 
 * @author Incode - Data: mai/2014
 * 
 */
@Resource
@Path("/antigo/idcpc000")
public class Idcpc000 {
	// Views utilizadas
	SicControleImp controle = new SicControleImp();
	FileReader arquivo1;
	String impPcl;

	private String i01;
	private String i02;
	private String i03;
	private String i04;
	Result result;
	Sessao sessao;

	public Idcpc000(Result result, Sessao sessao) {
		this.result = result;
		this.sessao = sessao;
	}

	@Post
	public void executar() {
		try {
			main();
		} catch (Exception e) {
			throw new ExceptionProgram(e, sessao, this.getClass().getSimpleName());
		}
	}

	private void main() throws IOException {
		File file = new File(UtilBean.pathFiles() + "arquivo1");
		// creates the file
		if (!file.exists()) {
			file.createNewFile();
		}
		arquivo1 = new FileReader(UtilBean.pathFiles() + "arquivo1");
		BufferedReader buf = new BufferedReader(arquivo1);

		while (controle.leitura()) {
			controle.exclui();
			// //dao.commit();
		}

		while ((impPcl = buf.readLine()) != null) {// READCOMS
			i01 = UtilBean.subStr(impPcl, 0, 249);
			i02 = UtilBean.subStr(impPcl, 249, 249);
			i03 = UtilBean.subStr(impPcl, 498, 249);
			i04 = UtilBean.subStr(impPcl, 747, 53);
			controle.setCampoA(i01);
			controle.setCampoB(i02);
			controle.setCampoC(i03);
			controle.setCampoD(i04);
			controle.grava();
			// //dao.commit();
		}
		arquivo1.close();
	}
}
